package com.cookor.poi.modules.phy.web;

import com.cookor.poi.common.config.Global;
import com.cookor.poi.common.persistence.Page;
import com.cookor.poi.common.utils.DateUtils;
import com.cookor.poi.common.utils.StringUtils;
import com.cookor.poi.common.utils.excel.ExportExcel;
import com.cookor.poi.common.web.BaseController;
import com.cookor.poi.modules.phy.entity.FSchedueTemplate;
import com.cookor.poi.modules.phy.entity.TOrder;
import com.cookor.poi.modules.phy.service.FSchedueTemplateService;
import com.cookor.poi.modules.phy.service.TOrderService;
import com.cookor.poi.modules.phy.service.TPatientService;
import com.cookor.poi.modules.sys.entity.Dict;
import com.cookor.poi.modules.sys.utils.UserUtils;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping({"${adminPath}/phy/tOrder"})
public class TOrderController extends BaseController {
  @Autowired
  private TOrderService tOrderService;
  
  @Autowired
  private TPatientService tPatientService;
  
  @Autowired
  private FSchedueTemplateService fSchedueTemplateService;
  
  @ModelAttribute
  public TOrder get(@RequestParam(required = false) String id) {
    TOrder entity = null;
    if (StringUtils.isNotBlank(id))
      entity = this.tOrderService.get(id); 
    if (entity == null)
      entity = new TOrder(); 
    return entity;
  }
  
  @RequiresPermissions({"phy:tOrder:view"})
  @RequestMapping({"list", ""})
  public String list(TOrder tOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
    tOrder.setTenantId(UserUtils.getTenantId());
    Page<TOrder> page = this.tOrderService.findPage(new Page(request, response), tOrder);
    model.addAttribute("page", page);
    model.addAttribute("tOrder", tOrder);
    return "modules/phy/tOrderList";
  }
  
  @RequiresPermissions({"phy:tOrder:view"})
  @RequestMapping({"onlineList"})
  public String onlineList(TOrder tOrder, HttpServletRequest request, HttpServletResponse response, Model model) {
    tOrder.setTenantId(UserUtils.getTenantId());
    tOrder.setOnlineType(Integer.valueOf(1));
    Page<TOrder> page = this.tOrderService.findPage(new Page(request, response), tOrder);
    model.addAttribute("page", page);
    model.addAttribute("tOrder", tOrder);
    return "modules/phy/tOrderOnlineList";
  }
  
  @RequiresPermissions({"phy:tOrder:view"})
  @RequestMapping({"form"})
  public String form(TOrder tOrder, Model model) {
    model.addAttribute("tOrder", tOrder);
    return "modules/phy/tOrderForm";
  }
  
  @RequiresPermissions({"phy:tOrder:view"})
  @RequestMapping({"onlineForm"})
  public String onlineForm(TOrder tOrder, Model model) {
    List<Dict> dictList = new ArrayList<>();
    FSchedueTemplate fSchedueTemplate = new FSchedueTemplate();
    fSchedueTemplate.setDelFlag("0");
    List<FSchedueTemplate> list = this.fSchedueTemplateService.findList(fSchedueTemplate);
    int i = 1;
    for (FSchedueTemplate bean : list) {
      Dict dict = new Dict();
      dict.setValue(String.valueOf(i++));
      dict.setLabel(bean.getTitle() + "(" + bean.getBeginDate() + "-" + bean.getEndDate() + ")");
      dictList.add(dict);
    } 
    model.addAttribute("dictList", dictList);
    model.addAttribute("tOrder", tOrder);
    return "modules/phy/tOrderOnlineForm";
  }
  
  @RequiresPermissions({"phy:tOrder:edit"})
  @RequestMapping({"save"})
  public String save(TOrder tOrder, Model model, RedirectAttributes redirectAttributes) {
    if (!beanValidator(model, tOrder, new Class[0]))
      return form(tOrder, model); 
    this.tOrderService.save(tOrder);
    addMessage(redirectAttributes, new String[] { "保存订单成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tOrder/?repage";
  }
  
  @RequiresPermissions({"phy:tOrder:edit"})
  @RequestMapping({"onlineSave"})
  public String onlineSave(Integer patientId, String patientName, String patientCard, String bookDate, Integer type, RedirectAttributes redirectAttributes) throws Exception {
    if (patientId == null || StringUtils.isBlank(patientName) || StringUtils.isBlank(patientCard) || 
      StringUtils.isBlank(bookDate) || type == null)
      throw new Exception("参数有误"); 
    this.tOrderService.onlineSave(patientId, bookDate, type);
    addMessage(redirectAttributes, new String[] { "保存订单成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tOrder/onlineList?repage";
  }
  
  @RequiresPermissions({"phy:tOrder:edit"})
  @RequestMapping({"cancel"})
  public String cancel(TOrder tOrder, RedirectAttributes redirectAttributes) {
    tOrder.setStatus(Integer.valueOf(11));
    this.tOrderService.save(tOrder);
    addMessage(redirectAttributes, new String[] { "取消成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tOrder/onlineList?repage";
  }
  
  @RequiresPermissions({"phy:tOrder:edit"})
  @RequestMapping({"delete"})
  public String delete(TOrder tOrder, RedirectAttributes redirectAttributes) {
    this.tOrderService.delete(tOrder);
    addMessage(redirectAttributes, new String[] { "删除订单成功" });
    return "redirect:" + Global.getAdminPath() + "/phy/tOrder/?repage";
  }
  
  @RequiresPermissions({"phy:tOrder:view"})
  @RequestMapping(value = {"export"}, method = {RequestMethod.POST})
  public String exportFile(TOrder tOrder, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
    try {
      String fileName = "预约数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
      Page<TOrder> page = this.tOrderService.findPage(new Page(request, response), tOrder);
      (new ExportExcel("预约数据", TOrder.class)).setDataList(page.getList()).write(response, fileName).dispose();
      return null;
    } catch (Exception e) {
      e.printStackTrace();
      addMessage(redirectAttributes, new String[] { "导出预约数据失败！失败信息：" + e.getMessage() });
      return "redirect:" + this.adminPath + "/phy/tOrder/?repage";
    } 
  }
}
